package com.sharad.quizbowl.ui.server;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Joiner;
import com.sharad.quizbowl.ui.dao.jdbc.ResourceManager;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class DataServlet extends HttpServlet {

	private static final long serialVersionUID = 3558767490085646678L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {

		PrintWriter writer = null;
		response.setContentType("text/javascript;charset=utf-8");
		String alt = request.getParameter("alt");
		try {
			writer = response.getWriter();
			XStream xstream = null;
			List<Integer> years = new ArrayList<Integer>();
			List<String> tournaments = new ArrayList<String>();
			List<String> difficulties = new ArrayList<String>();
			List<String> categories = new ArrayList<String>();
			int numUsers = 0, numQuestions = 0, numScores = 0;
			Connection conn = ResourceManager.getConnection();

			PreparedStatement s = conn
					.prepareStatement("select distinct year from tossups order by year desc");
			ResultSet set = s.executeQuery();
			while (set.next()) {
				years.add(set.getInt("year"));
			}
			s = conn.prepareStatement("select distinct year,tournament from tossups order by year desc, tournament asc");
			set = s.executeQuery();
			while (set.next()) {
				tournaments.add(Joiner.on(" ").join(set.getInt("year"),
						set.getString("tournament")));
			}
			s = conn.prepareStatement("select distinct difficulty from tossups");
			set = s.executeQuery();
			while (set.next()) {
				difficulties.add(set.getString("difficulty"));
			}
			s = conn.prepareStatement("select distinct category from tossups");
			set = s.executeQuery();
			while (set.next()) {
				categories.add(set.getString("category"));
			}
			s = conn.prepareStatement("select count(*) from tossups");
			set = s.executeQuery();
			while (set.next()) {
				numQuestions = set.getInt(1);
			}
			s = conn.prepareStatement("select count(*) from usernames");
			set = s.executeQuery();
			while (set.next()) {
				numUsers = set.getInt(1);
			}
			s = conn.prepareStatement("select count(*) from scores");
			set = s.executeQuery();
			while (set.next()) {
				numScores = set.getInt(1);
			}
			conn.close();
			DataPackage data = new DataPackage();
			data.load(years, tournaments, difficulties, categories,
					numQuestions, numUsers, numScores);
			if (alt == null) {
				xstream = new XStream(new DomDriver());
				xstream.setMode(XStream.NO_REFERENCES);
				xstream.alias("data", DataPackage.class);
				writer.print(xstream.toXML(data));
			} else if (alt.equals("json-in-script")) {
				xstream = new XStream(new JsonHierarchicalStreamDriver());
				xstream.setMode(XStream.NO_REFERENCES);
				xstream.alias("data", DataPackage.class);
				writer.print((request.getParameter("callback") == null ? "callback"
						: request.getParameter("callback"))
						+ "(");
				writer.print(xstream.toXML(data));
				writer.print(")");
			}
		} catch (Throwable e) {
			writer.print("Error: query did not work!\n");
			writer.print(e.getMessage());

		}
		writer.flush();
		writer.close();
	}
}
